package com.billow.control
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	public class FileControl
	{
		private var _fileStream:FileStream = new FileStream();
		private var _file:File;
		
		private var _tempBytes:ByteArray;
		private var _char:String;
		private var _bitmap:Bitmap;
		
		private var _loader:Loader;
		private var _loading:Boolean;
		private var _imageComplete:Function;
		
		public function FileControl()
		{
			super();
		}
		
		/** 当前 File */
		public function get file():File {return _file;}
		
		/** 当前Bitmap */
		public function get bitmap():Bitmap {return _bitmap;}
		
		
		/**
		 * 获取字符串 (UTF)
		 * @param file
		 * @return 
		 * 
		 */		
		public function readUTF(file:File):String
		{
			removeBytes();
			
			_file = file;
			_fileStream.open(_file ,FileMode.READ);
			_char = _fileStream.readUTFBytes(_fileStream.bytesAvailable);
			_fileStream.close();
			
			BSystem.gc();
			return _char;
		}
		
		
		/**
		 * 获取二进制 
		 * @param file
		 * @return 
		 * 
		 */		
		public function readBytes(file:File):ByteArray
		{
			removeBytes();
			
			_file = file;
			_fileStream.open(_file ,FileMode.READ);
			_tempBytes = new ByteArray();
			_fileStream.readBytes(_tempBytes);
			_fileStream.close();
			
			BSystem.gc();
			return _tempBytes;
		}
		
		
		
		
		
		/**
		 * 加载图片 
		 * @param file
		 * @param callback
		 * 
		 */		
		public function loadBitmap(file:File ,complete:Function):void
		{
			removeBytes();
			removeLoader();
			
			_file = file;
			_imageComplete = complete;
			_tempBytes = readBytes(_file);
			
			_loading = true;
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,onComplete);
			_loader.loadBytes(_tempBytes);
		}
		
		private function onComplete(evt:Event):void
		{
			removeBytes();
			
			_loading = false;
			_bitmap = _loader.content as Bitmap;
			_imageComplete();
		}
		
		
		
		
		
		
		
		
		/**
		 * 保存文件 返回true为保存成功  返回false为保存失败 
		 * @param file
		 * @param bytes
		 * @return 
		 * 
		 */				
		public function saveFile(file:File ,bytes:ByteArray):Boolean
		{
			try{
				_file = file;
				_fileStream.open(_file, FileMode.WRITE);
				_fileStream.writeBytes(bytes);
				_fileStream.close();
				return true;
			}catch(e:Error){
				
			}
			
			return false;
		}
		
		
		
		
		
		/** 回收 BytesArrar */
		private function removeBytes():void
		{
			_char = null;
			
			if(_tempBytes!=null)
			{
				_tempBytes.clear();
				_tempBytes = null;
			}
		}
		
		/** 回收 Bitmap 以及 Loader */
		private function removeLoader():void
		{
			if(_bitmap!=null)
			{
				_bitmap.bitmapData.dispose();
				_bitmap = null;
			}
			
			if(_loader!=null)
			{
				if(_loading)
				{
					try{
						_loader.close();
					}catch(e:Error){}
				}
				_loading = false;
				_loader.unloadAndStop();
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE ,onComplete);
				_loader = null;
			}
		}
		
		
		
		
		/**
		 * 回收 
		 * 
		 */		
		public function dispose():void
		{
			_file = null;
			if(_fileStream!=null)
			{
				_fileStream.close();
				_fileStream = null;
			}
			removeBytes();
			removeLoader();
			BSystem.gc();
		}
	}
}